<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

// Loading all the components
$undo_redo = $_GET["undo_redo"];

if(!isset($semf_root) )
{
	$semf_root = '../';
	// Define that we are returning XML content & not to cache:
	header('Content-Type: text/xml');
	header('Cache-control: no-cache');
}

if($undo_redo == "undo" || $undo_redo == "redo")
{
	/** this have been first defined in ./util/backup.php */
	$backup_dirname = "/backup/";
	$backup_postfix = ".bk";
	$ver_postfix = ".ver";
	$meta_postfix = ".meta";
	$ver_limit = 50;
	
	$operantion_successful = false;
	
	$path_name = dirname($page_xml);
	$file_name = basename($page_xml);
	
	$backup_dir = $path_name.$backup_dirname;
	$meta_filename = $backup_dir.$file_name.$meta_postfix;
	if(file_exists($meta_filename) && filesize($meta_filename) > 0)
	{
		$filesize = filesize($meta_filename);
		$meta_file = fopen($meta_filename, "r");
		$contents = fread($meta_file, $filesize);
		$sep = strpos($contents, ",");
		$current = substr($contents, 0, $sep);
		$rest = substr($contents, $sep+1);
		$sep = strpos($rest, ",");
		$max = substr($rest, 0, $sep);
		$min = substr($rest, $sep + 1);
		
		fclose($meta_file);
		
		if($undo_redo == "undo")
		{
			$new_current = undo($current, $max, $min, $ver_limit);
		}
		elseif($undo_redo == "redo")
		{
			$new_current = redo($current, $max, $min, $ver_limit);
		}
		
		if($new_current != -1) //so continue..
		{
			unlink($meta_filename);
			$meta_file = fopen($meta_filename, "w");
			fwrite($meta_file, $new_current.",".$max.",".$min);
			fclose($meta_file);
			
			//identify the real copy
			$page_xml_ver = $backup_dir.$file_name.$ver_postfix.$new_current;
			
			
			copy($page_xml_ver, $page_xml);
			
			$operantion_successful = true;
		}
	}
	if(!$operantion_successful)
	{
		//here just to undo and redo no need to reload things
		//exit();
	}
}

/** return 
 * the undoable current version if undoable
 * -1 if not undoable
 */
function undo($current, $max, $min, $ver_limit)
{
	if($min == -1)
	{
		return $current -1; //if $current == 0 just return -1
	}
	else
	{
		$current--;
		if($current == -1)
		{
			$current == $ver_limit;
		}
		if($min != $current)
		{
			return $current;
		}
		return -1;
	}
	return -1;
}

/** return 
 * the redoable current version if redoable
 * -1 if not redoable
 */
function redo($current, $max, $min, $ver_limit)
{
	if($current == $max)
	{
		return -1;
	}
	//just generate a linear address.
	$current ++;
	if($current == $ver_limit)
	{
		$current = 0;
	}
	return $current;
}
?>